package JUC辅助类;
//信号量

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *   限流玩法
 *
 *
 */
public class SemaphoreTest {
    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(3);
        score score = new score();

//        获取前3名
        for (int i = 1; i <= 10000; i++) {
            new Thread(() -> {

                try {
                    //        获取 名额
                    semaphore.acquire();
//                    具体操作 判断他是第几个获取
                    System.out.println("谁："+Thread.currentThread().getName()+"第"+score.add()+"个拿到的");

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }finally {
                    //        释放  代参数 permits  释放几个
                    semaphore.release();
                }
            },i+"").start();
        }




    }


    static class score{

        private int num = 0;

        Lock lock = new ReentrantLock();

        public int add(){

//            写时加锁
//            lock.lock();
            ++num;
//            lock.unlock();


            return num;
        }
    }
}
